package com.dt76.smallloanbackenduser.controller.etao;

import com.dt76.smallloanbackenduser.pojo.etao.Meun;
import com.dt76.smallloanbackenduser.remote.etao.MenuRemote;
import com.dt76.smallloanbackenduser.remote.etao.RoleManageRemote;
import com.dt76.smallloanbackenduser.utils.MenuList;
import com.dt76.smallloanbackenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/menuManage")
public class MenuManageController {

    @Autowired
    private MenuRemote menuRemote;
    @Autowired
    private RoleManageRemote roleManageRemote;

    @RequestMapping("/getMenuByrId")
    public String getMenuByrId(Model model, Integer rId) {
        //调用远程方法查询本rId的所有权限
        System.out.println("要查询的角色id---"+rId);
        List<String> menuByRId = menuRemote.getMenuByRId(rId);
        //得到所有权限
        List<Meun> allMenu = menuRemote.getAllMenu();
        //将权限返回给页面
        model.addAttribute("allMenu", allMenu);
        model.addAttribute("menuByRId", menuByRId);
//        model.addAttribute("rId", rId);
        model.addAttribute("currentRole", roleManageRemote.getRoleById(rId));
        for (String meun : menuByRId) {
            System.out.println(meun);
        }
        return "pages/etao/menuManage";
    }

    @RequestMapping("/editMenu")
    @ResponseBody
    public Result editMenuByRId(MenuList menuList, Integer rId) {
        menuList.setrId(rId);
        Result result = menuRemote.editMenu(menuList);
        System.out.println(result.isSuccess());
        if (result.isSuccess()) {
            result.setMsg("设置成功");
        } else {
            result.setMsg("设置失败");
        }
        return result;
    }

    @RequestMapping("/getMenu")
    @ResponseBody
    public String getMenu(Integer rId) {
        return "/menuManage/getMenuByrId?rId=" + rId;
    }


}
